/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-11-13     ganze       the first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <drv_common.h>
#include <drivers/pin.h>
#include <drv_spi.h>
#include <rtdbg.h>
#include "app_lsm6dsl.h"
#include "sensor_st_lsm6dsl.h"

int rt_lsm6dsl_sensor_register(void)
{
    struct rt_sensor_config cfg;

    cfg.intf.dev_name = "i2c2";
    cfg.intf.user_data = (void *)LSM6DSL_ADDR_DEFAULT;
    cfg.irq_pin.pin = RT_PIN_NONE;

    rt_hw_lsm6dsl_init("lsm6dsl", &cfg);
    return 0;
}
// INIT_APP_EXPORT(rt_lsm6dsl_sensor_register);

int lsm6dsl_acce_start(rt_device_t *p_acce_dev)
{
    rt_device_t dev = RT_NULL;

    dev = rt_device_find("acce_lsm6dsl"); // 查找设备
    if (dev)
    {
        rt_kprintf("find lsm6dsl acce\n");
    }

    if (rt_device_open(dev, RT_DEVICE_FLAG_RDONLY) == RT_EOK)
    { // 打开设备
        rt_kprintf("OPEN lsm6dsl acce RT_DEVICE_FLAG_RDONLY OK\n");
    }

    *p_acce_dev = dev;
    return RT_EOK;
}

int lsm6dsl_gyro_start(rt_device_t *p_gyro_dev)
{
    rt_device_t dev = RT_NULL;

    dev = rt_device_find("gyro_lsm6dsl"); // 查找设备
    if (dev)
    {
        rt_kprintf("find lsm6dsl gyro\n");
    }

    if (rt_device_open(dev, RT_DEVICE_FLAG_RDONLY) == RT_EOK)
    { // 打开设备
        rt_kprintf("OPEN lsm6dsl gyro RT_DEVICE_FLAG_RDONLY OK\n");
    }

    *p_gyro_dev = dev;
    return RT_EOK;
}

// // SPI设备需要先注册到SPI总线，即先声明一个片选线
// static int lsm6dsl_spi_css(void)
// {
//     // 挂载SPI设备lsm6dsl
//     if (rt_hw_spi_device_attach(LSM6DSL_SPI_BUSNAME, LSM6DSL_SPI_DEVNAME, GPIOB, GPIO_PIN_11) != RT_EOK)
//     {
//         return -1;
//     }
//     return 0;
// }
// INIT_COMPONENT_EXPORT(lsm6dsl_spi_css);
